package fr.compil.tab.model;

import java.util.ArrayList;
import java.util.Stack;
import java.util.regex.Pattern;

import javax.management.InvalidAttributeValueException;

import fr.compil.tab.logic.CellManager;

public class Cell {
	private boolean isCommand;
	private String rawValue;
	private Integer processedValue;
	private ArrayList<Cell> linkedCells;
	private int columnIndex;
	private int rowIndex;
	private Board linkedBoard;
	
	public Cell()
	{
	}
	
	public Cell(int rowIndex, int columnIndex, Board linkedBoard){
		this.rowIndex = rowIndex;
		this.columnIndex = columnIndex;
		this.linkedBoard = linkedBoard;
	}
	
	public void AddLinkedCell(Cell cellToAdd)
	{
		linkedCells.add(cellToAdd);
	}
	
	public void RemoveLinkedCell(Cell cellToRemove)
	{
		linkedCells.remove(cellToRemove);
	}
	
	public boolean isCommand() {
		return isCommand;
	}
	public void setCommand(boolean isCommand) {
		this.isCommand = isCommand;
	}
	public String getRawValue() {
		return rawValue;
	}
	public void setRawValue(String rawValue) {
		this.rawValue = rawValue;
	}
	public Integer getProcessedValue() {
		return processedValue;
	}
	public void setProcessedValue(Integer i) {
		this.processedValue = i;
	}
	public ArrayList<Cell> getLinkedCells() {
		return linkedCells;
	}
	public void setLinkedCells(ArrayList<Cell> linkedCells) {
		this.linkedCells = linkedCells;
	}
	public int getColumnIndex() {
		return columnIndex;
	}
	public void setColumnIndex(int columnIndex) {
		this.columnIndex = columnIndex;
	}
	public int getRowIndex() {
		return rowIndex;
	}
	public void setRowIndex(int rowIndex) {
		this.rowIndex = rowIndex;
	}
	
	Pattern cellPattern = Pattern.compile("[a-zA-Z]+[1-9]+[0-9]*");
	Pattern operatorPattern = Pattern.compile("\\+|\\-|\\*|\\/");
	Pattern numberPattern = Pattern.compile("[0-9]+");
	Pattern functionPattern = Pattern.compile("^\\(.+\\)$");
	
	public void refreshValue() throws InvalidAttributeValueException
	{
		if(getRawValue() == null || getRawValue() == "") return;
		int pValue = CellManager.processTokenStack(CellManager.sliceValue(getRawValue())); 
		setProcessedValue(pValue);
	}
}
